package a_doubleWrite;

/**
 * todo
 *
 * @author luolidong
 * @version 1.0
 * @create 2021-07-21 11:47
 */
public class L43_plus {
    public String multiply(String num1, String num2) {
        //将每个位存放到数组中
        int[] nums = new int[num1.length() + num2.length()];

        for (int i = num1.length() - 1; i >= 0; i--) {
            int n1 = num1.charAt(i) - '0';
            for (int j = num2.length() - 1; j >= 0; j--) {
                int n2 = num2.charAt(j) - '0';
                int sum = (n1 * n2 + nums[i + j + 1]);
                nums[i + j + 1] = sum % 10;
                nums[i + j] += sum / 10;//注意 i+j相同元素共用nums[i + j]
            }
        }
        //从头开始拼接
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < nums.length; i++) {
            //第一个元素可能不存在，跳过
            if (i == 0 && nums[i] == 0) continue;
            builder.append(nums[i]);
        }
        return builder.toString();
    }
}
